﻿#include "CAlgorithmImageHy.h"
#include "CHeader.h"
#include  "CLogOper.h"
#include <QtGlobal>
#include "CSqlOper.h"
CAlgorithmImageHy::CAlgorithmImageHy(QString sSceneId)
{
	m_sGuid = sSceneId;//场景id
	m_dLastLocation = -60;
	m_dLastConLocation = -60;
	m_bConllectBegin = false;
}

CAlgorithmImageHy::~CAlgorithmImageHy()
{
	killTimer(m_nExecTimer);
}

bool CAlgorithmImageHy::actionAlgorthm(SCPolItemCom * pPolItemCom, QString sTaskName)
{
	m_pRBasicData->m_pMutex->lock();
	CPolItemCamer *pPolItemCamer = (CPolItemCamer*)pPolItemCom->m_pPolItem;
	if (pPolItemCamer->m_nCheckType == 1)
	{
		if (pPolItemCamer->m_nCollectIndex == 0)
		{
			m_bConllectBegin = true;
			m_nExecTimer = startTimer(50);
			m_pPolItemCom = pPolItemCom;
			pPolItemCom->m_bIsSave = true;
		}
		if (pPolItemCamer->m_nCollectIndex == 1)
		{
			killTimer(m_nExecTimer);
			pPolItemCom->m_bIsSave = true;
		}
		if (pPolItemCamer->m_nCollectIndex == 2)
		{
			m_pPolItemCom = pPolItemCom;
			pPolItemCom->m_bIsSave = true;
		}
		m_pRBasicData->m_pMutex->unlock();
	}
	else
	{
		m_pRBasicData->m_pMutex->unlock();
		execAlgorthm(pPolItemCom, sTaskName);
	}

	return true;
}

void CAlgorithmImageHy::closeCotDet()
{
	m_pPolItemCom = nullptr;
	if (m_nExecTimer != 0)
	{
		killTimer(m_nExecTimer);
	}
}

bool CAlgorithmImageHy::execAlgorthm()
{
	m_pRBasicData->m_pMutex->lock();
	CPolItemCamer *pPolItemCamer = (CPolItemCamer*)m_pPolItemCom->m_pPolItem;//云台巡检项
	if (m_pPolItemCom == nullptr || pPolItemCamer == nullptr)
	{
		m_pRBasicData->m_pMutex->unlock();
		return false;
	}
	/**
	* @ Author: Gaojia
	* @ Description: 获取基础数据
	*/
	CDevDataBase* pDevDataRob = (CDevDataBase*)m_pDev->m_pDevDataBase;
	CDevDataCam* pDataDevCamer = pDevDataRob->m_pDevDataCam;
	double dCurrLocation = pDevDataRob->m_sRobCurrLoca.toFloat() / 1000;
	double dRealLoaction = pPolItemCamer->m_dRollerOffdetDis + dCurrLocation;
	QString sRollerDesc = "";   //托辊名称
	int nRollerNum = 0;         //托辊序号
	nRollerNum = pDevDataRob->getCurrRollerNo(sRollerDesc);//连续检测结果的当前所在托辊号
	/**
	* @ Author: Gaojia
	* @ Description: 1.判断是否需要采集数据
	*/
	if (pPolItemCamer->m_nCollectCheckType == 0)
	{
		double dInterval = pPolItemCamer->m_dCollectInterval;
		if (!isCollectMeter(nRollerNum, dInterval))
		{
			m_pRBasicData->m_pMutex->unlock();
			return false;
		}
	}
	else
	{
		int nInterval = pPolItemCamer->m_nCollectRollerNum;
		if (!isCollectRoller(nRollerNum, nInterval))
		{
			m_pRBasicData->m_pMutex->unlock();
			return false;
		}
	}
	double dValue = pDataDevCamer->m_dAtcTempMax;
	/**
	* @ Author: Gaojia
	* @ Description: 3.生成数据
	*/
	CItemResultCamera *pItemResultCamer = (CItemResultCamera *)m_pPolItemCom->m_ItemResult;//相机结果
	CItemResultCamera *pItemResultCamerCon = new CItemResultCamera();
	pItemResultCamer->copyData(pItemResultCamerCon);
	pItemResultCamerCon->m_sItemResultGuid = createGuid();//生成新的巡检项结果Guid
	pItemResultCamerCon->m_dRealLocation = dCurrLocation;//连续检测结果的当前所在位置m
	pItemResultCamerCon->m_dValue = dValue;
	pItemResultCamerCon->m_dActionTime = QDateTime::currentDateTime();    //执行时间
	pItemResultCamerCon->m_sRollerDesc = sRollerDesc;
	pItemResultCamerCon->m_nRollerNum = nRollerNum;
	pItemResultCamerCon->m_sTaskName = m_pRBasicData->m_pTaskExecInfo->m_sTaskName;
	m_pPolItemCom->m_pPolPoint->m_sRollerDesc = sRollerDesc;
	m_pPolItemCom->m_pPolPoint->m_nRollerCode = nRollerNum;
	getAlarmData(pItemResultCamerCon);
	/**
	* @ Author: Gaojia
	* @ Description: 4.调用图像相机拍照
	*/
	QString sImgUrl = pDataDevCamer->m_sRecordPath + "Image/";
	pItemResultCamerCon->m_sItemName = m_pPolItemCom->m_pPolPoint->m_sRollerDesc + "-" + A2T("图像检测");
	m_pRBasicData->m_pMutex->unlock();
	if (actPicture(sImgUrl))
	{
		m_pRBasicData->m_pMutex->lock();
		pItemResultCamerCon->m_sIRUrl = sImgUrl;
		pItemResultCamerCon->m_sHttpUrl = CPublicData::instance().m_sHttpUrl + sImgUrl.remove(pDataDevCamer->m_sRecordPath);
		m_pRBasicData->m_pMutex->unlock();
	}
	m_pRBasicData->m_pMutex->lock();
	if (pItemResultCamerCon->m_nState != 0)
	{
		//更新上次发生报警位置
		m_dLastLocation = dCurrLocation;
		m_pRBasicData->m_pTaskExecInfo->m_nCollectAlarmCount++;
	}
	m_pRBasicData->m_pTaskExecInfo->m_nCollectExecCount++;
	QString sGuid = pItemResultCamerCon->m_sTaskExecGuid;
	int nAlarmCount = m_pRBasicData->m_pTaskExecInfo->m_nCollectAlarmCount;
	int nExecCount = m_pRBasicData->m_pTaskExecInfo->m_nCollectExecCount;
	m_pRBasicData->m_pMutex->unlock();
	updateCollectCountEx(sGuid, nAlarmCount, nExecCount);
	insertCamerItemResult(pItemResultCamerCon);//连续检测红外信息插入数据库云台巡检项结果表
	m_pRBasicData->m_pMutex->lock();
	m_pPolItemCom->m_vecItemResult.push_back(pItemResultCamerCon);
	m_pRBasicData->m_pMutex->unlock();
	return true;
}

bool CAlgorithmImageHy::execAlgorthm(SCPolItemCom * pPolItemCom, QString sTaskName)
{
	m_pRBasicData->m_pMutex->lock();
	CDevDataBase* pDevDataRob = (CDevDataBase*)m_pDev->m_pDevDataBase;
	CDevDataCam* pDataDevCamer = pDevDataRob->m_pDevDataCam;
	CItemResultCamera *pItemResultCamera = (CItemResultCamera *)pPolItemCom->m_ItemResult;
	double dCurrLocation = pDevDataRob->m_sRobCurrLoca.toFloat() / 1000;
	QString sRollerDesc = "";   //托辊名称
	int nRollerNum = 0;         //托辊序号
	nRollerNum = pDevDataRob->getRollerNo(dCurrLocation, sRollerDesc, pItemResultCamera->m_nBelong);//当前所在托辊号
	pItemResultCamera->m_dRealLocation = dCurrLocation;//连续检测结果的当前所在位置m
	pItemResultCamera->m_dActionTime = QDateTime::currentDateTime();
	pItemResultCamera->m_nState = 0;
	pItemResultCamera->m_nAlarmLevel = 0;
	pItemResultCamera->m_sDataDesc = A2T("可见光拍照");
	pItemResultCamera->m_sRollerDesc = sRollerDesc;
	pItemResultCamera->m_nRollerNum = nRollerNum;
	pPolItemCom->m_pPolPoint->m_sRollerDesc = sRollerDesc;
	pPolItemCom->m_pPolPoint->m_nRollerCode = nRollerNum;
	m_pRBasicData->m_pMutex->unlock();
	return true;
}

void CAlgorithmImageHy::timerEvent(QTimerEvent * event)
{
	if (event->timerId() == m_nExecTimer)
	{
		try
		{
			execAlgorthm();
		}
		catch (const std::exception&)
		{

		}
		
	}
}

void CAlgorithmImageHy::getAlarmData(CItemResultCamera * pItemResultCamera)
{
	pItemResultCamera->m_nState = 0;
	pItemResultCamera->m_nAlarmLevel = 0;
	pItemResultCamera->m_sDataDesc = A2T("可见光拍照");
}

bool CAlgorithmImageHy::actPicture(QString & sImgUrl)
{
	SCtrInfo info;
	info.m_bCtrSync = true;
	info.m_sGuid = m_sGuid;
	info.m_bHasCtr = false;
	info.m_nCtrDevType = enSubDev1;
	info.m_nCtrCmd = enCCapture;
	info.m_nSubCtrDevType = enCamer;
	info.m_sImgUrl = sImgUrl;
	if (m_pDev->ctrDev(info))
	{
		sImgUrl = info.m_sImgUrl;
		return true;
	}
	else
	{
		return false;
	}
}
